class_name Player
extends CharacterBody2D

@onready var player: CharacterBody2D = $"."
@onready var animation_tree: AnimationTree = $AnimationTree

@export var current_tool : DataTypes.Tools = DataTypes.Tools.None

var lastDirection : Vector2 = Vector2.DOWN

const SPEED = 2000.0

func _ready() -> void:
	ToolsMgr.tool_selected.connect(on_tool_selected)

func on_tool_selected(tool: DataTypes.Tools) -> void:
	current_tool = tool
	$HitComponent.current_tool = tool

func _physics_process(delta: float) -> void:
	var direction := GameInputEvents.movement_input()

	player.velocity = direction * SPEED * delta
	
	var idle = direction == Vector2.ZERO
	var hit = Input.is_action_just_pressed("hit") and current_tool != DataTypes.Tools.None

	animation_tree.set("parameters/Player State/conditions/hit", hit)
	animation_tree.set("parameters/Player State/conditions/idle", idle)
	animation_tree.set("parameters/Player State/conditions/walk", !idle)
	
	if !idle:
		lastDirection = direction

	animation_tree.set("parameters/Player State/Idle/blend_position", lastDirection)
	animation_tree.set("parameters/Player State/Walk/blend_position", lastDirection)
	animation_tree.set("parameters/Player State/Work/blend_position", lastDirection)

	if hit:
		animation_tree.set("parameters/TimeScale/scale", 1)
	else:
		animation_tree.set("parameters/TimeScale/scale", 2)
	
	player.move_and_slide()
